package com.apptonelabs.android.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PNRStatus implements Serializable {
	static final long serialVersionUID =5250948754941694501L;
	
	private long id;
	private String pnr;
	private long doj = -1;
	private TrainDetails trainDetails;
	private List<PassengerDetails> passengerDetails = new ArrayList<PassengerDetails>();
	
	public String toString() {
		StringBuffer b = new StringBuffer();
		b.append("PNR:" + this.pnr);
		b.append("Train Number:" + trainDetails.getNumber());
		b.append("Train name:" + trainDetails.getName());
		b.append("boardingDate: " + trainDetails.getBoardingDate());
		b.append("Passengers count: " + passengerDetails.size());
		b.append("chart status : " + trainDetails.getChatStatus());
		return b.toString();
	}

	public String getDOJStr() {
		if(this.doj != -1) {
			return (new Date(this.doj).toString());
		} else {
			return "NA";
		}
	}
	
	public void addPassenger(PassengerDetails item) {
		this.passengerDetails.add(item);
	}
	
	/**
	 * @return the trainDetails
	 */
	public TrainDetails getTrainDetails() {
		return trainDetails;
	}

	/**
	 * @param trainDetails
	 *            the trainDetails to set
	 */
	public void setTrainDetails(TrainDetails trainDetails) {
		this.trainDetails = trainDetails;
	}

	/**
	 * @return the passengerDetails
	 */
	public List<PassengerDetails> getPassengerDetails() {
		return passengerDetails;
	}

	/**
	 * @param passengerDetails
	 *            the passengerDetails to set
	 */
	public void setPassengerDetails(List<PassengerDetails> passengerDetails) {
		this.passengerDetails = passengerDetails;
	}

	public String getPnr() {
		return pnr;
	}

	public void setPnr(String pnr) {
		this.pnr = pnr;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getDoj() {
		return doj;
	}

	public void setDoj(long doj) {
		this.doj = doj;
	}
	
	public static byte[] serialize(Object obj) throws IOException {
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ObjectOutputStream os = new ObjectOutputStream(out);
	    os.writeObject(obj);
	    return out.toByteArray();
	}

	public static PNRStatus deserialize(byte[] data) throws IOException, ClassNotFoundException {
	    ByteArrayInputStream in = new ByteArrayInputStream(data);
	    ObjectInputStream is = new ObjectInputStream(in);
	    return (PNRStatus) is.readObject();
	}
}
